package vng.wcc.sonph.LauncherGenerator;

import java.util.Calendar;

import android.content.Context;
import android.text.TextUtils.TruncateAt;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewConfiguration;
import android.widget.LinearLayout;

public class CellView extends LinearLayout {

	private static final int COLOR_ORANGE = 0xffffd700;

	private static final long MAX_CLICK_TIME = 500;

	private IconView icon;
	private CellInfo info;
	private Formatter formatter;
	private TitleView title;

	private long pressedTime;

	private long releasedTime;

	/**
	 * CellView is a cell on the screen
	 * @param context
	 * @param formatter provides layout information 
	 * @param info information about the cell: icon,title, and intent to launch
	 */
	public CellView(Context context,Formatter formatter,CellInfo info) {
		super(context);
		this.info = info;
		this.formatter = formatter;
		initView();

	}

	private void initView() {
		setViewLayout();
		addChildren();
	}

	private void addChildren() {
		this.addIconView();
		this.addTitleView();
	}

	private void addTitleView() {
		//Set attributes for the title view
		this.title = new TitleView(this.getContext());
		title.setPadding(
				formatter.getTitleLeftPadding(),
				formatter.getTitleRightPadding(),
				formatter.getTitleTopPadding(),
				formatter.getTitleBottomPadding());
		title.setText(info.getTitle());
		title.setGravity(Gravity.CENTER_HORIZONTAL);
		title.setEllipsize(TruncateAt.END);
		title.setFadingEdgeLength(10);
//		title.setTextColor(getResources().getColor(android.R.color.black));
//		title.setBackgroundResource(android.R.color.darker_gray);					

		this.addView(
				title, 
				formatter.getTitleWidth(),
				formatter.getTitleHeight());
		this.title.setOnTouchListener(new OnTouchListener() {
			
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				//pass to the parent
				return false;
			}
		});
		//Add intent to launch on item click
//		this.title.setOnClickListener(new OnCellClickListener());

	}

	private void addIconView() {
		this.icon = new IconView(this.getContext());

		icon.setPadding(
				formatter.getIconLeftPadding(), 
				formatter.getIconTopPadding(),
				formatter.getIconRightPadding(),
				formatter.getIconBottomPadding()
		);
		icon.setImageBitmap(info.getIconImage());
		icon.setBackgroundResource(android.R.color.transparent);
		
		this.addView(
				icon, 
				formatter.getIconLayoutWidth(), 
				formatter.getIconLayoutHeight());
		//Add intent to launch on item click
		this.icon.setOnTouchListener(new OnTouchListener() {			
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				// pass to parent
				return false;
			}
		});
//		this.icon.setOnClickListener( new OnCellClickListener());			


	}

	private void setViewLayout() {
		int cellPadding = formatter.getCellPadding();
		this.setPadding(cellPadding,cellPadding,cellPadding,cellPadding);
		this.setOrientation(LinearLayout.VERTICAL);

	}	

//	private class OnCellClickListener implements OnClickListener {
//
//		@Override
//		public void onClick(View v) {	
//			//TODO: why doesnt this code work ?
//			CellView.this.getContext().startActivity(info.getIntent());
//		}
//				
//	}
	@Override
	public boolean onTouchEvent (MotionEvent event) {
		Calendar now = Calendar.getInstance();
		float 
			prs_posx = 0,
			prs_posy = 0,
			rel_posx = 0,
			rel_posy = 0;
		int touchSlop = ViewConfiguration.get(getContext()).getScaledTouchSlop();
		switch (event.getAction()) {
		case MotionEvent.ACTION_DOWN:
			this.setBackgroundColor(COLOR_ORANGE);
			pressedTime = now.getTimeInMillis();
			prs_posx = event.getX();
			prs_posy = event.getY();
			return true;
		case MotionEvent.ACTION_CANCEL:
			this.setBackgroundResource(android.R.color.transparent);
			return true;
		case MotionEvent.ACTION_UP:
			releasedTime = now.getTimeInMillis();
			this.setBackgroundResource(android.R.color.transparent);
			if (releasedTime - pressedTime < MAX_CLICK_TIME ) {
				this.getContext().startActivity(info.getIntent());
			}			
			return true;	
		default: return super.onTouchEvent(event);
		}				
	}

}

